#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define _CRT_SECURE_NO_WARNINGS 1
int map[4][4] = { {0} };
int width = 4;
int height = 4;
int ret = 0;
//设计背景 打印数字
void bkbord(int map[4][4])
{
    int i = 0, j = 0, k = 0;
    printf("|-----+-----+-----+------\n");
    for (i = 0;i < width;i++) {
        for (j = 0;j < height;j++) {
            if (map[i][j] == 0) {
                printf("|     ");
            }
            else {
                printf("|%5d", map[i][j]);
            }
        }printf(" |\n");
        printf("|-----+-----+-----+------|\n");
    }


}
//初始化数据
void creat(int map[4][4])
{
    int a, b;
    srand(time(0));
    a = rand() % 4;
    b = rand() % 4;
    map[a][b] = 2;
    a = rand() % 4;
    b = rand() % 4;
    while (map[a][b] == 2)
    {
        a = rand() % 4;
        b = rand() % 4;
    }
    map[a][b] = 2;
}


//随机生成2或4
void make(int map[4][4])
{
    int a, b, c, d;
    srand(time(0));
    a = rand() % 2;
    c = rand() % 4;
    d = rand() % 4;
    while (map[c][d] != 0) {
        c = rand() % 4;
        d = rand() % 4;
    }
    if (a == 0) {
        b = 2;
        map[c][d] = b;
    }
    else {
        b = 4;
        map[c][d] = b;
    }


}
//移动
void moveleft(int map[4][4])
{
    int i = 0, j = 1;
    for (i = 0;i < width; i++) {
        int a = 0;
        for (j = 1;j < height;j++) {
            if (map[i][j] != 0) {
                if (map[i][a] == 0) {
                    map[i][a] = map[i][j];
                    map[i][j] = 0;
                }
                else if (map[i][a] == map[i][j]) {
                    map[i][a] *= 2;
                    map[i][j] = 0;

                }
                else
                {
                    map[i][a + 1] = map[i][j];
                    if (a + 1 != j) {
                        map[i][j] = 0;//为a下一行时无需变值
                    }
                    a++;
                }
            }
        }
    }
}
void moveright(int map[4][4]) {
    int i = 0, j = 2;
    for (i = 0;i < width; i++) {
        int a = 3;
        for (j = 2;j > -1;j--) {
            if (map[i][j] != 0) {
                if (map[i][a] == 0) {
                    map[i][a] = map[i][j];
                    map[i][j] = 0;
                }
                else  if (map[i][a] == map[i][j]) {
                    map[i][a] *= 2;
                    map[i][j] = 0;
                }
                else {
                    map[i][a - 1] = map[i][j];
                    if (a - 1 != j) {
                        map[i][j] = 0;
                    }
                    a--;
                }
            }
        }
    }

}
void moveup(int map[4][4]) {
    int i = 1, j = 0;
    for (j = 0;j < height; j++) {
        int a = 0;
        for (i = 1;i < width;i++) {
            if (map[i][j] != 0) {
                if (map[a][j] == 0) {
                    map[a][j] = map[i][j];
                    map[i][j] = 0;
                }
                else if (map[a][j] == map[i][j])
                {
                    map[a][j] += map[i][j];
                    map[i][j] = 0;
                }
                else {
                    map[a + 1][j] = map[i][j];
                    if (a + 1 != i) {
                        map[i][j] = 0;
                    }
                    a++;
                }
            }

        }
    }

}
void movedown(int map[4][4]) {
    int i = 2, j = 0;
    for (j = 0;j < height; j++) {
        int a = 3;
        for (i = 2;i > -1;i--) {
            if (map[i][j] != 0) {
                if (map[a][j] == 0) {
                    map[a][j] = map[i][j];
                    map[i][j] = 0;
                }
                else if (map[a][j] == map[i][j]) {
                    map[a][j] *= 2;
                    map[i][j] = 0;
                }
                else {
                    map[a - 1][j] = map[i][j];
                    if (a - 1 != i) {
                        map[i][j] = 0;
                    }
                    a--;
                }
            }

        }
    }
}
int judge(int map[4][4]) {
    int i = 0, j = 0, ret = 0;
    while (ret != 1 && ret != 2) {
        for (i = 0;i < width;i++) {
            for (j = 0;j < height;j++) {
                if (map[i][j] == 2048) {
                    ret = 1;
                }
            }
        }
        for (i = 0;i < width;i++) {
            for (j = 0;j < height;j++) {
                if (map[i][j] == 0) {
                    ret = 2;
                }
            }
        }

        for (i = 0;i < width;i++) {
            for (j = 0;j < height - 1;j++) {
                if (map[i][j] == map[i][j + 1]) {
                    ret = 2;
                }
            }

        }
        for (j = 0;j < height;j++) {
            for (i = 0;i < width - 1;i++) {
                if (map[i][j] == map[i + 1][j]) {
                    ret = 2;
                }
            }
        }
        if (ret == 0) {
            break;
        }
    }
    return ret;
}
int judgeagain() {
    int i, j;
    for (i = 0;i < width;i++) {
        for (j = 0;j < height;j++) {
            if (map[i][j] == 0) {
                return 1;
            }

        }
    }
}

int main()
{
        creat(map);
        bkbord(map);
        char a;
        a = _getch();
        switch (a) {
        case 'w':
            moveup(map);
            ret = judge(map);
            break;
        case 's':
            movedown(map);
            ret = judge(map);
            break;
        case 'a':
            moveleft(map);
            ret = judge(map);
            break;
        case 'd':
            moveright(map);
            ret = judge(map);
            break;
        }
        ret = judge(map);
        while (ret != 1 && ret != 0) {
            if (judgeagain() == 1) { make(map); }
            system("cls");
            bkbord(map);
            a = _getch();
            switch (a) {
            case 'w':
                moveup(map);
                ret = judge(map);
                break;
            case 's':
                movedown(map);
                ret = judge(map);
                break;
            case 'a':
                moveleft(map);
                ret = judge(map);
                break;
            case 'd':
                moveright(map);
                ret = judge(map);
                break;
            } system("cls");
            bkbord(map);
        }
        if (ret == 1) {
            printf("成功");
        }if (ret == 0) {
            printf("失败");
        }
        return 0;
    }
   
